package com.hlt.hltapps;




import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.glt.hltapps.frg.FragmentCart;
import com.glt.hltapps.frg.FragmentMain;
import com.glt.hltapps.frg.FragmentPerson;
import com.glt.hltapps.frg.FragmentSearch;
import com.glt.hltapps.frg.FragmentSort;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnCheckedChange;

public class MainActivity extends FragmentActivity {

	@ViewInject(R.id.radioGroup)
	RadioGroup radioGroup;
	
	@ViewInject(R.id.rb_main)
	RadioButton rb_main;
	
	
	@ViewInject(R.id.layout_frame)
	FrameLayout layout_frame;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ViewUtils.inject(this);
	}
	
	
	
	/**
	 * 当Activity已经初始化完毕时
	 */
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		rb_main.setChecked(true);
	}

	@OnCheckedChange({R.id.radioGroup})
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		int index = 0;
		switch (checkedId) {
		case R.id.rb_main:
			index = 10;
			break;
		case R.id.rb_sort:
			index = 20;
			break;
		case R.id.rb_search:
			index = 30;
			break;
		case R.id.rb_cart:
			index = 40;
			break;
		case R.id.rb_person:
			index = 50;
			break;
		}
		Fragment fragment = 
(Fragment) fragmentAdapter.instantiateItem(layout_frame, index);
		
		fragmentAdapter.setPrimaryItem(layout_frame, 0, fragment);
		fragmentAdapter.finishUpdate(layout_frame);
	}

	
	FragmentStatePagerAdapter fragmentAdapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
		@Override
		public Fragment getItem(int arg0) {
			switch (arg0) {
			case 10:
				return new FragmentMain();
			case 20:
				return new FragmentSort();
			case 30:
				return new FragmentSearch();
			case 40:
				return new FragmentCart();
			case 50:
				return new FragmentPerson();
			default:
				return null;
			}
		}
		@Override
		public int getCount() {
			return 5;
		}
		
	};
	

}
